$(function(){
	parent.IndexPage.toggleLocker('close'); // 沉浸式体验
	$("#searchArticle").on('keydown', function(event) {
		if (event.keyCode == 13) {
			// 回车
			var originalName = $("#searchArticle").val();
			$("#findFileInfoFrm").find("input[name='originalName']").val(originalName);
			findFileInfoAjax.send();
		}
		if (event.keyCode == 27) {
			// ESC
			var originalName = $("#searchArticle").val();
			if (StringUtil.isNotBlank(originalName)) {
				$("#searchArticle").val('');
				$("#findFileInfoFrm").find("input[name='originalName']").val('');
				findFileInfoAjax.send();
			}
		}
	});
});
var FileListPage = {
	'onFileChanged':function(ele) {
		
	},
	'onUploadSucc':function(stat) {
		stat = JSON.parse(stat);
		if(stat.success == true){
			$.messager.info("文件上传成功！");
			fileInfoDatatable.refresh();
		} else {
			$.messager.error("文件上传失败！");
		}
	},
	'onQuerySucc':function(data, status, xhr) {
		
	},
	// 删除文件
	'deleteFile':function(item, e, rowNumber) {
		MsgBox.confirm("确认删除文件：" + item.originalName + "?", null, function() {
			fileDeleteAjax.tempUrl(function(url){
				return url + "/" + item.fileId;
			});
			fileDeleteAjax.send();
		});
	},
	// 文件删除成功
	'onFileDeleteSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		var mWindow = MsgBox.getCurrentOpenDialogWindow();
		mWindow.fileInfoDatatable.refresh();
	},
	// 下载文件
	'downloadFile':function(item, e, rowNumber) {
		$.download({
			'data':{
				'fileId':item.fileId,
				'download':true
			},
			'url':ctx + "/datum/file/fileDownload"
		});
		FileListPage.updateDownloadCount(item.fileId, rowNumber); // 更新下载次数
	},
	// 通过超链接下载文件，更新下载次数
	'updateDownloadCount':function(fileId, rowNumber) {
		updateDownloadCountAjax.tempUrl(function(url){
			return url + "/" + fileId + "/" + rowNumber;
		});
		updateDownloadCountAjax.send();
	},
	// 更新下载次数成功
	'onUpdateDownloadCountSucc':function(data, status, xhr) {
		var obj = data.info; // obj.fileId, obj.row
		var row = obj.row;
		var $downloadCount = $("#fileInfoDatatable").find("tr[row-index='" + row + "']").find("td[column-key='downloadCount'] p");
		$downloadCount.text(parseInt($downloadCount.text()) + 1);
	},
	// 渲染每一项
	'onRenderItem':function(colName,text,indexStr,mItem, rowNumber) {
		switch(colName) {
		case 'visitLink':
			var url = "http://" + serverName + ":8080/files/" + mItem.fileId + "." + FilenameUtils.getExtension(mItem.originalName);
			return $("<a href='" + url + "' target='_blank' onclick='FileListPage.updateDownloadCount(" + mItem.fileId + ", " + rowNumber + ")'>" + url + "</a>");
		case 'originalName':
			var url = "http://" + serverName + ":8080/files/" + mItem.fileId + "." + FilenameUtils.getExtension(mItem.originalName);
			return $("<a href='" + url + "' target='_blank' onclick='FileListPage.updateDownloadCount(" + mItem.fileId + ", " + rowNumber + ")'>" + mItem.originalName + "</a>");
		}
	}
};